package com.example.homework.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.homework.db.tables.Words
import com.example.homework.repositories.WordRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch

class WordViewModel(val repository: WordRepository) : ViewModel() {
    val firstFive: Flow<List<Words>> = repository.words.stateIn(
        scope = viewModelScope,
        started = SharingStarted.WhileSubscribed(5000L),
        initialValue = listOf()
    )

    fun addWord(word: String) {
        viewModelScope.launch {
            repository.addWord(word)
        }
    }

    fun cleanAll() {
        viewModelScope.launch {
            repository.clearAll()
        }
    }
}